package com.koi.system.controller.operlog;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.koi.common.anno.KoiLog;
import com.koi.common.enums.log.OperType;
import com.koi.common.ex.koi.YUtil;
import com.koi.common.utils.string.StringUtil;
import com.koi.model.vo.system.SysOperLogVo;
import com.koi.pojo.system.SysOperLog;
import com.koi.system.service.oper.SysOperLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ･ᴗ･
 * @description [操作日志-控制层]
 */
@Validated
@RequestMapping("/koi")
@RequiredArgsConstructor
@RestController
public class SysOperLogController {

    private final SysOperLogService sysOperLogService;

    /**
     * @description [分页查询]
     * @author ･ᴗ･
     */
    @SaCheckPermission("system:operlog:list")
    @GetMapping("/sysOperLog/listPage")
    public IPage<SysOperLog> listPage(SysOperLogVo vo) {
        // 分页构造器
        Page<SysOperLog> page = new Page<>(vo.getPageNo(), vo.getPageSize());
        // 条件构造器
        LambdaQueryWrapper<SysOperLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtil.isNotBlank(vo.getOperName()), SysOperLog::getOperName, vo.getOperName());
        wrapper.like(StringUtil.isNotBlank(vo.getOperMan()), SysOperLog::getOperMan, vo.getOperMan());
        wrapper.like(StringUtil.isNotBlank(vo.getOperIp()), SysOperLog::getOperIp, vo.getOperIp());
        wrapper.ge(vo.getBeginTime() != null, SysOperLog::getOperTime, vo.getBeginTime()); // 大于等于
        wrapper.le(vo.getEndTime() != null, SysOperLog::getOperTime, vo.getEndTime()); // 小于等于
        wrapper.orderByDesc(SysOperLog::getOperId);
        // 进行分页查询
        page = sysOperLogService.page(page, wrapper);
        return page;
    }

    /**
     * @description [查询所有]
     * @author ･ᴗ･
     */
    @GetMapping("/sysOperLog/list")
    public List<SysOperLog> list() {
        return sysOperLogService.list();
    }

    /**
     * @description [查询一个]
     * @author ･ᴗ･
     */
    @GetMapping("/sysOperLog/getById/{id}")
    public SysOperLog getById(@PathVariable Long id) {
        return sysOperLogService.getById(id);
    }

    /**
     * @description [新增]
     * @author ･ᴗ･
     */
    @PostMapping("/sysOperLog/add")
    public void add(@Validated @RequestBody SysOperLog sysOperLog) {
        YUtil.isTrue(!sysOperLogService.save(sysOperLog), "新增失败，请稍后重试");
    }

    /**
     * @description [修改]
     * @author ･ᴗ･
     */
    @PostMapping("/sysOperLog/update")
    public void update(@Validated @RequestBody SysOperLog sysOperLog) {
        YUtil.isTrue(!sysOperLogService.updateById(sysOperLog), "修改失败，请稍后重试");
    }

    /**
     * @description [删除]
     * @author ･ᴗ･
     */
    @KoiLog(title = "删除操作日志", operType = OperType.DELETE)
    @SaCheckPermission("system:operlog:delete")
    @PostMapping("/sysOperLog/deleteById/{id}")
    public void delete(@PathVariable Long id) {
        YUtil.isTrue(!sysOperLogService.removeById(id), "删除失败，请稍后重试");
    }

    /**
     * @description [批量删除]
     * @author ･ᴗ･
     */
    @KoiLog(title = "删除操作日志", operType = OperType.DELETE)
    @SaCheckPermission("system:operlog:delete")
    @PostMapping("/sysOperLog/batchDelete")
    public void batchDelete(@RequestBody List<Long> ids) {
        YUtil.isTrue(!sysOperLogService.removeBatchByIds(ids), "删除失败，请稍后重试");
    }

    /**
     * @description [查询详情]
     * @author ･ᴗ･
     */
    @GetMapping("/sysOperLog/getDetailById/{id}")
    public SysOperLog getDetailById(@PathVariable("id") Long id) {
        LambdaQueryWrapper<SysOperLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(
                SysOperLog::getMethodName,
                SysOperLog::getOperParam,
                SysOperLog::getJsonResult,
                SysOperLog::getOperStatus,
                SysOperLog::getErrorMsg,
                SysOperLog::getCostTime
        );
        wrapper.eq(SysOperLog::getOperId, id);
        return sysOperLogService.getOne(wrapper);
    }

}
